//
//  SearchSingerViewController.m
//  艾听歌
//
//  Created by dllo on 16/5/31.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "SearchSingerViewController.h"
#import "photoModel.h"
#import "SearchTableViewCell.h"
#import "SingerViewController.h"

@interface SearchSingerViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic , strong) UITableView *tableView;
@property (nonatomic , assign) NSInteger page;
@end

@implementation SearchSingerViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.dk_backgroundColorPicker = DKColorPickerWithRGB(0xffffff,0x9C9C9C);
     self.navigationController.navigationBar.dk_barTintColorPicker = DKColorPickerWithRGB(0xFF6EB4, 0x9C9C9C);
    self.singerArr = [NSMutableArray array];
//    self.page = 1;
    [self createTableView];
//    [self dataHandlerSearchSinger];
}

#pragma mark - 解析数据(歌手)
- (void)dataHandlerSearchSinger:(NSString *)searchText withNextPage:(NSInteger)currentPage{
    NSString *str = [NSString stringWithFormat:@"http://search.dongting.com/artist/search?q=%@&page=%ld&size=15",searchText,currentPage];
    [NetworkHandle GetDataWithURL:str dic:nil success:^(id responseObject) {
        if (self.page == 1) {
            self.singerArr = [NSMutableArray array];
        }
//        [self.tableView.footer endRefreshing];
        NSArray *array = responseObject[@"data"];
//        [self.singerArr addObjectsFromArray:[photoModel transformWithArray:array]];
        for (NSDictionary *smallDic in array) {
            photoModel *model = [[photoModel alloc]init];
            
            [model setValuesForKeysWithDictionary:smallDic];
            [self.singerArr addObject:model];
        }
        [self.tableView reloadData];
    } filed:^(NSError *error) {
//        [self.tableView.footer endRefreshing];
    }];
}

- (void)setSearchText:(NSString *)searchText
{
    _searchText = searchText;
    NSLog(@"%@", self.searchText);
    self.page = 1;
    [self dataHandlerSearchSinger:_searchText withNextPage:1];
    [self.tableView reloadData];
    [self addFooter:_searchText];
    [self.tableView reloadData];
    
}

- (void)addFooter:(NSString *)searchText{
    
    __block SearchSingerViewController *root = self;
//    [self.tableView addLegendFooterWithRefreshingBlock:^{
//        root.page++;
        [root dataHandlerSearchSinger:searchText withNextPage:root.page];
//    }];
    
}
- (void)createTableView{
    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:(UITableViewStylePlain)];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.rowHeight = 100;
    [self.view addSubview:_tableView];
    [self.tableView registerClass:[SearchTableViewCell class] forCellReuseIdentifier:@"cell"];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.singerArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    SearchTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    photoModel *singerRankModel = _singerArr[indexPath.row];
    [cell.imgView sd_setImageWithURL:[NSURL URLWithString:singerRankModel.pic_url]];
    cell.imgView.contentMode = UIViewContentModeScaleAspectFill;
    
    cell.name.text = singerRankModel.singer_name;
    cell.dk_backgroundColorPicker = DKColorPickerWithRGB(0xffffff,0x9C9C9C);
    if (self.searchText.length) {
        NSMutableAttributedString *attrituteString = [[NSMutableAttributedString alloc] initWithString:singerRankModel.singer_name];
        // 获取标红的位置和长度
        NSRange range = [singerRankModel.singer_name rangeOfString:self.searchText options:NSCaseInsensitiveSearch];
        
        // 设置标签文字的属性
        [attrituteString setAttributes:@{NSForegroundColorAttributeName : [UIColor redColor],   NSFontAttributeName : [UIFont systemFontOfSize:17]} range:range];
        // 显示在Label上
        cell.name.attributedText = attrituteString;
    }
    
    
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    return cell;
}

//- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
//    
//    [tableView deselectRowAtIndexPath:indexPath animated:YES];
//    
//    [self.searchBar resignFirstResponder];
//    
//    SingerViewController *singerInfoVC = [[SingerViewController alloc] init];
//    photoModel *singerRankModel = self.singerArr[indexPath.row];
//    
//    singerInfoVC.singerId = singerRankModel.mId;
//    singerInfoVC.singerName = singerRankModel.singer_name;
//    [self.navi pushViewController:singerInfoVC animated:YES];
//}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
